package request;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.encrypt.SimpleAES;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import com.feidee.fdcommon.util.TimeUtils;
import com.feidee.fdcommon.util.ToolUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/10.
 */
public class SimulateRequest {

	@Test
	public void tagMetaDataTest() throws Exception {


		String data = "kpi_ssj_daily_report";
		String iteminfo = JSON.toJSONString(data);

		String path = "/feidee-data-report/offlinereport.do";//接口路径
		String account = "UNIT_TEST";
		String ak_secret = ToolUtils.getAkSecret(account);
		System.out.println(ak_secret);
		String ciphertext = SimpleAES.encrypt(iteminfo, ak_secret);//密文
		String date = TimeUtils.toGMTString(new Date());//时间
		String stringToSign = ToolUtils.MD5Base64(iteminfo) + "\n" + date + "\n" + path;
		String signature = account + ":" + ToolUtils.HMACSha1(stringToSign, ak_secret);//签名
		String url = "http://10.195.1.151:8668/feidee-data-report/offlinereport.do";
		HttpPost httppost = null;
		try {
			httppost = new HttpPost(url);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("data", ciphertext));
			UrlEncodedFormEntity urlData = new UrlEncodedFormEntity(nvps, Charset.forName("UTF-8"));
			httppost.setHeader("Date", date);
			httppost.setHeader("Authorization", signature);
			httppost.setEntity(urlData);
			StringResponse response = HttpUtil.post(httppost);
			System.out.println(response.getResponseBody());
		} finally {
			if (httppost != null)
				httppost.releaseConnection();
		}
	}

}
